-module(push_server).
-behavior(gen_server).
-export([start_link/0,init/1,handle_call/3,handle_cast/2,handle_info/2,terminate/2,code_change/3]).
-export([register/1,push/1]).
-define(SERVER,?MODULE).
start_link() ->
	gen_server:start_link({local, ?SERVER}, ?MODULE,[],[]).
init(_) ->
	io:format("push server is start~n"),
	{ok,[]}.
	
handle_call({register,Pid},_,State) ->
	{reply,ok,[Pid|State]};
	
handle_call({deregister,Pid},_,State) ->
	{reply,ok,lists:delete(Pid,State)};
	
handle_call({evt,Message},_,State) ->
	lists:foreach(fun(Pid) ->
					yaws_api:websocket_send(Pid,{text,list_to_binary(Message)})
				  end,
				  State
				),
	{reply,ok,State};
handle_call(_,_,State) ->
	{reply, {error,"no func define"}, State}.
handle_cast(_, State) ->
	{noreply,State}.
handle_info(_, State) ->
	{noreply,State}.
terminate(_, _) ->
	ok.
code_change(_, State, _) ->
	{ok,State}.	
register(Pid) ->
	gen_server:call(?SERVER,{register,Pid}).
push(Evt) ->
	gen_server:call(?SERVER,Evt).